<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button
            v-allow="'import_excel'"
            type="warning"
            size="small"
            @click="$router.push('import')"
          >excel导入</el-button>
          <el-button
            v-allow="'export_excel'"
            type="danger"
            size="small"
            @click="handleDownload"
          >excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click.native="doAddEmp"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="图像" prop="staffPhoto">
            <template slot-scope="scope">
              <!-- <img
                :src="scope.row.staffPhoto"
                alt=""
                style="width: 100%; height: 100%"
              > -->
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template slot-scope="scope">
              {{ format(scope.row.formOfEmployment) }}
              <!-- <button @click="f(scope)" /> -->
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="账户状态" prop="correctionTime" />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="hAssign(scope.row.id)"
              >分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="del(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog :visible.sync="showDialog" title="新增员工">
        <em-dialog
          @close="showDialog = false"
          @hclose="Close"
          @success="Hsuccess"
        />
      </el-dialog>
      <el-dialog v-if="showDialogRole" :visible.sync="showDialogRole">
        <AssignRole :curid="curid" @Hsuccess="AssignSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import AssignRole from './assignRole.vue'
import emDialog from './empDialog.vue'
const EMPE_CODE = { 1: '正式', 2: '非正式' }
export default {
  components: {
    emDialog,
    AssignRole
  },
  data() {
    return {
      list: [],
      page: 1,
      pagesize: 2,
      total: 0,
      showDialog: false,
      showDialogRole: false,
      curid: ''
    }
  },
  created() {
    this.loadEmployeeList()
    this.format()
  },
  methods: {
    AssignSuccess() {
      this.showDialogRole = false
      this.$message.success('修改权限成功')
    },
    hAssign(id) {
      this.showDialogRole = true
      this.curid = id
      console.log(id)
    },
    // 导出
    async handleDownload() {
      const res = await getEmployeeList({
        page: this.page,
        size: this.pagesize
      })
      const { header, data } = this.formatExcel(res.data.rows)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header: header, // 表头 必填
          data: data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    formatExcel(arr) {
      // const mapinfo = {
      //   id: '编号',
      //   password: '密码',
      //   mobile: '手机号',
      //   username: '姓名',
      //   timeOfEntry: '入职日期',
      //   formOfEmployment: '聘用形式',
      //   correctionTime: '转正日期',
      //   workNumber: '工号',
      //   departmentName: '部门',
      //   staffPhoto: '头像地址'
      // }
      // const enkeys = Object.keys(arr[0])

      // const list = arr.map((item) => {
      //   return {
      //     id: item.id,
      //     password: item.password,
      //     mobile: item.mobile,
      //     username: item.username,
      //     timeOfEntry: item.timeOfEntry,
      //     formOfEmployment: EMPE_CODE[item.formOfEmployment],
      //     correctionTime: item.correctionTime,
      //     workNumber: item.workNumber,
      //     departmentName: item.departmentName,
      //     staffPhoto: item.staffPhoto
      //   }
      // })
      const list = arr.map((item) => {
        return {
          编号: item.id,
          密码: item.password,
          手机号: item.mobile,
          姓名: item.username,
          入职日期: item.timeOfEntry,
          聘用形式: EMPE_CODE[item.formOfEmployment],
          转正日期: item.correctionTime,
          工号: item.workNumber,
          部门: item.departmentName,
          头像地址: item.staffPhoto
        }
      })
      // console.log(list)
      const header = Object.keys(list[0])
      const data = list.map((item) => Object.values(item))
      return { header, data }
    },

    Hsuccess() {
      this.total++
      this.page = Math.ceil(this.total / this.pagesize)
      this.showDialog = false
      this.$message.success('操作成功')
    },
    Close() {
      this.showDialog = false
    },
    doAddEmp() {
      this.showDialog = true
    },
    async del(id) {
      await delEmployee(id)
      if (this.list.length === this.pagesize) {
        this.page--
      }
      this.$message.success('删除成功')
      this.loadEmployeeList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadEmployeeList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.loadEmployeeList()
    },
    format(type) {
      return EMPE_CODE[type] || '未知'
    },
    f(scope) {
      console.log(scope)
    },
    async loadEmployeeList() {
      const res = await getEmployeeList({
        page: this.page,
        size: this.pagesize
      })
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    }
  }
}
</script>
<style scoped>
img {
  border-radius: 50%;
}
</style>
